package com.example.works;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class EX03_Intent_02 extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex03_intent02);
        tv_result = findViewById(R.id.tv_result);
        //获取intent中的bundle
        Bundle bundle = getIntent().getExtras();
        String sex;
        sex = bundle.getString("sex");
        double height;
        height = Integer.parseInt(bundle.getString("height"));
        double weight;
        weight = Integer.parseInt(bundle.getString("weight"));
        //计算BMI,判断后生成返回字符串
        double bmi;
        bmi = (weight / (height * height * 0.0001));
        //String strBmi = String.format("%.2f", bmi);
        String bodyStatus;
        double wt_01;
        double wt_02;
        String str1 = "您合适的体重范围为";
        String str2 = "身高质量指数为";
        String access;
        String access_01 = "需要疯狂星期四!";
        String access_02 = "注意补充营养";
        String access_03 = "请继续保持!";
        String access_04 = "管住嘴,迈开腿";
        String access_05 = "疫验丁真, 鉴定为:_____";
        String access_06 = "请输入正常人类的身高和体重";
        String result;

        if (sex == "man") {
            //计算体重范围
            wt_01 = 20.7*height*height*0.0001;
            wt_02 = 26.4*height*height*0.0001;

            //判断身体状况
            if (bmi < 17.5) {
                bodyStatus = "属于厌食";
                access = access_01;
            } else if (bmi < 20.7 && bmi >= 17.5) {
                bodyStatus = "属于体重偏轻";
                access = access_02;

            } else if (bmi < 26.4 && bmi >= 20.7) {
                bodyStatus = "属于正常范围";
                access = access_03;

            } else if (bmi < 27.8 && bmi >= 26.4) {
                bodyStatus = "属于略微超重";
                access = access_04;
            } else if (bmi >= 27.8) {
                bodyStatus = "属于超重";
                access = access_05;
            } else {
                bodyStatus = "异于常人";
                access = access_06;
            }
        }

        //女
        else {
            //计算体重范围
            wt_01 = 19.1*height*height*0.0001;
            wt_02 = 25.8*height*height*0.0001;

            //判断身体状况
            if (bmi < 17.5) {
                bodyStatus = "属于厌食";
                access = access_01;

            } else if (bmi < 19.1 && bmi >= 17.5) {
                bodyStatus = "属于体重偏轻";
                access = access_02;

            } else if (bmi < 25.8 && bmi >= 19.1) {
                bodyStatus = "属于正常范围";
                access = access_03;

            } else if (bmi < 27.3 && bmi >= 25.8) {
                bodyStatus = "属于略微超重";
                access = access_04;

            } else if (bmi >= 27.3) {
                bodyStatus = "属于超重";
                access = access_05;

            } else {
                bodyStatus = "异于常人";
                access = access_06;

            }
        }
        //String.format("%s%d~%dkg\n%s%d,%s\n%s",str1,wt_01,wt_02,str2,bmi,bodyStatus,access);
        //String wt_01_1 = String.format("%2d",wt_01);
        //String wt_02_1 = String.format("%2d",wt_02);
        //String bmi_1 = String.format("%2d",bmi);

        StringBuilder sb = new StringBuilder();
        sb.append(str1).append(String.format("%.2f",wt_01)).append("~").append(String.format("%.2f",wt_02)).append("kg").append('\n');
        sb.append(str2).append(String.format("%.2f",bmi)).append(bodyStatus).append(",").append('\n');
        sb.append(access);
        result = sb.toString();
        tv_result.setText(result);


        //返回
        findViewById(R.id.btn_01).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_01:
                Intent intent = new Intent(this, Ex03_Intent.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}